package alvarodelrosal.chat.server.infraestructura;

import alvarodelrosal.chat.server.modelo.ChatConversation;
import alvarodelrosal.chat.server.modelo.ChatMessage;
import alvarodelrosal.chat.server.modelo.ChatUser;
import java.util.List;

public class ChatPersistenceManager {

    private ChatUsersPersistence usersPersistence;
    private ChatsPersistence chatsPersistence;
    private static ChatPersistenceManager persistenceManager = null;
    
    private ChatPersistenceManager() {
        usersPersistence = ChatUsersDAO.getPersistence("db");
        chatsPersistence = ChatsDAO.getPersistence("db");
    }
    
    public static ChatPersistenceManager getPersistenceManager() {
        if(persistenceManager == null) {
            persistenceManager = new ChatPersistenceManager();
        }
        return persistenceManager;
    }
    
    public boolean existsTheUser(String username) {
        return usersPersistence.existsTheUser(username);
    }
    
    public boolean existsTheUser(String username, String password) {
        return usersPersistence.existsTheUser(username, password);
    }
    
    public ChatUser getTheUser(String username, String password) {
        return usersPersistence.getTheUser(username, password);
    }
    
    public ChatUser getTheUser(int userId) {
        return usersPersistence.getTheUser(userId);
    }
    
    public void addUser(ChatUser user) {
        usersPersistence.addUser(user);
    }
    
    public void deleteUser(int userId) {
        usersPersistence.deleteUser(userId);
    }
    
    public List<ChatUser> getAllUsers() {
        return usersPersistence.getAllUsers();
    }
    
    public List<Integer> getAllChats() {
        return chatsPersistence.getAllChats();
    }
    
    public String getChatName(int ChatId) {
        return chatsPersistence.getChatName(ChatId);
    }
    
    public List<ChatMessage> getMessagesFrom(int chatId) {
        return chatsPersistence.getMessagesFrom(chatId);
    }
    
    public List<Integer> getChatsWith(int userId) {
        return chatsPersistence.getChatsWith(userId);
    }
    
    public void save(ChatConversation conversation) {
        chatsPersistence.save(conversation);
    }
    
}
